package com.yuehuanghun.admin.core.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuehuanghun.admin.core.domain.SysDictData;
import com.yuehuanghun.admin.core.mapper.SysDictDataMapper;
import com.yuehuanghun.admin.core.service.ISysDictDataService;
import com.yuehuanghun.admin.core.service.ISysDictTypeService;
import com.yuehuanghun.mybatis.milu.ext.BaseServiceImpl;

/**
 * 字典 业务层处理
 * 
 * @author yadmin
 */
@Service
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictData, Long, SysDictDataMapper> implements ISysDictDataService {
	@Autowired
	private ISysDictTypeService sysDictTypeService;

	/**
	 * 批量删除字典数据信息
	 * 
	 * @param dictCodes 需要删除的字典数据ID
	 * @return 结果
	 */
	@Override
	public int deleteDictDataByIds(Long[] dictCodes) {
		List<Long> dictCodeList = Arrays.asList(dictCodes);
		List<SysDictData> list = domainMapper.findByIds(dictCodeList);
		if (list.isEmpty()) {
			return 0;
		}
		int row = domainMapper.deleteByIds(Arrays.asList(dictCodes));
		if (row > 0) {
			list.stream().map(SysDictData::getDictType).forEach(dictType -> {
				sysDictTypeService.reloadDictCache(dictType);
			});
		}
		return row;
	}

	/**
	 * 新增保存字典数据信息
	 * 
	 * @param dictData 字典数据信息
	 * @return 结果
	 */
	@Override
	public int insertDictData(SysDictData dictData) {
		int row = domainMapper.insert(dictData);
		if (row > 0) {
			sysDictTypeService.reloadDictCache(dictData.getDictType());
		}
		return row;
	}

	/**
	 * 修改保存字典数据信息
	 * 
	 * @param dictData 字典数据信息
	 * @return 结果
	 */
	@Override
	public int updateDictData(SysDictData dictData) {
		int row = domainMapper.updateById(dictData);
		if (row > 0) {
			sysDictTypeService.reloadDictCache(dictData.getDictType());
		}
		return row;
	}
}
